 package view.pane;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import controller.GameState;
import controller.human.User;

import view.Gui;

@SuppressWarnings("serial")
public class MainPane extends JPanel {
	Gui gui;
	public MainPane(Gui guiParam) {
		this.gui = guiParam;
		
		JPanel gameStatePanel = new JPanel();
		gameStatePanel.add(new JLabel("First round of the game"));
		
		Set<User> users = gui.getUsers();
		
		JPanel usersPanel = new JPanel(); 
		usersPanel.setLayout(new GridLayout((users.size()+2)/3,3));
		
		for (User user: users) {
			JButton selectButton = new JButton(user.getName());
			
			selectButton.addActionListener(new SelectActionListener(user));
			
			usersPanel.add(selectButton);
		}
		
		JPanel others = new JPanel();
		others.add(new JButton(new AbstractAction("Night") {
			public void actionPerformed(ActionEvent arg0) {
				gui.changeState(GameState.Night);
				gui.updatePane(new NightPane(gui));
			}
		}));
		others.add(new JButton(new AbstractAction("Dummy") {
			public void actionPerformed(ActionEvent arg0) {
				//gui.changeState(GameState.Voting);
				//gui.updatePane(new VotingPane(gui));
			}
		}));
		
		this.setLayout(new GridLayout(3,1));
		this.add(gameStatePanel);
		this.add(usersPanel);
		this.add(others);
	}

	private class SelectActionListener implements ActionListener {
		User user;
		SelectActionListener(User user) {
			this.user = user;
		}
		
		public void actionPerformed(ActionEvent e) {
			final JComponent[] inputs = new JComponent[] {
			                new JButton(new AbstractAction("DeleteDummy") {
								public void actionPerformed(ActionEvent arg0) {
									//
								}
			                }),
			                new JButton(new AbstractAction("ProposedDummy") {
								public void actionPerformed(ActionEvent arg0) {
									//
								}
			                })
			};
			JOptionPane.showMessageDialog(null, inputs, "Select attribute", JOptionPane.PLAIN_MESSAGE);			
		}
	}	
}
